<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class OrderClassCommand extends Command
{
    protected $signature = 'make:order';
    protected $description = '机动组-创建统一订单表-命令';


    /**
     * FunctionName：handle
     * Description：
     * Author：lwl
     */
    public function handle()
    {
        try {
            $project = config('app.name');
            $table = '$table';
            $bigIncrements = '$table->bigIncrements';
            $tinyInteger = '$table->tinyInteger';
            $string = '$table->string';
            $unsignedTinyInteger = '$table->unsignedTinyInteger';
            $unsignedBigInteger = '$table->unsignedBigInteger';
            $decimal = '$table->decimal';
            $unique = '$table->unique';
            $dateTime = '$table->dateTime';
            $randNumber = rand(100000,999999);
            $content = <<<EOF
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTableOrder extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('{$project}_orders', function (Blueprint $table) {
            $bigIncrements('id');
            $tinyInteger('status')->default(0)->comment('支付状态 ：0创建中 -10订单关闭 10待付款 15支付中 20已付款 25发货中 30已发货 40待评价（已结算） 50交易完成');
            $string('order_sn',30)->default('')->comment('订单编号');
            $unsignedBigInteger('member_id')->default(0)->comment('会员id');
            $decimal('amount',10,4)->default(0)->comment('支付金额');
            $unsignedTinyInteger('pay_type')->default(0)->comment('1银联支付,2支付宝支付,3微信支付,4余额,5支付宝h5支付,6微信H5支付');
            $dateTime('payed_at')->nullable()->comment('完成支付时间');
            $string('remark')->default('')->comment('备注');
            $unsignedTinyInteger('close_type')->default(1)->comment('关闭类型 1自动关闭,2手动关闭');
            $unsignedTinyInteger('client')->default(0)->comment('下单源 10安卓 11iOS; 12WAP 13小程序 20PC');
            $string('close_reason')->default('')->comment('关闭原因');
            $dateTime('created_at')->nullable()->comment('创建日期');
            $dateTime('updated_at')->nullable()->comment('更新日期');
            $dateTime('deleted_at')->nullable()->comment('删除日期');
            $unique('order_sn');
        });
        \Illuminate\Support\Facades\DB::statement("ALTER TABLE `{$project}_orders` COMMENT='订单'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('{$project}_orders');
    }
}

EOF;
            $res = fopen(database_path("migrations/2021_07_12_{$randNumber}_create_order_table.php"), 'w');
            fputs($res, $content);
            fclose($res);
            $this->info('创建成功');
        }catch (\Exception $exception){
            $this->error('创建失败');
        }
    }
}
